package com.sinya.shoppingmall;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.OvershootInterpolator;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sinya.shoppingmall.utils.IntoUtils;
import com.sinya.shoppingmall.widget.address.AddressBean;
import com.sinya.shoppingmall.widget.address.AnimationListView;

import java.util.ArrayList;
import java.util.List;

public class MyAddressActivity extends AppCompatActivity {

    public Context context;
    private AnimationListView listview;
    private List<AddressBean> addressBeans;
    private AddressAdapter addressAdapter;
    private TextView tv_address_add;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_address);
        this.context = MyAddressActivity.this;
        addressBeans = new ArrayList<AddressBean>();
        initViews();
    }

    private void initViews() {

        TextView title = (TextView) findViewById(R.id.tv_title);
        title.setText("地址管理");

        ImageView back = (ImageView) findViewById(R.id.img_back);
        back.setImageResource(R.drawable.btn_title_back_selector);
        back.setVisibility(View.VISIBLE);
        back.setOnClickListener(onClick);

        tv_address_add = (TextView) findViewById(R.id.tv_address_add);
        tv_address_add.setOnClickListener(onClick);

        listview = (AnimationListView) findViewById(R.id.ll_listView);
        addressAdapter = new AddressAdapter();
        listview.setAdapter(addressAdapter);
        listview.setAnimationDurationFactor(3.0f);
        listview.setInterpolater(new OvershootInterpolator());

    }

    View.OnClickListener onClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            switch (v.getId()) {

                case R.id.tv_address_add:
                    //TODO 添加地址
                    IntoUtils.goActivityForResult(MyAddressActivity.this, MyAddressAddedActivity.class, 0);
                    break;

                case R.id.img_back:
                    finish();
                    break;

            }
        }
    };

    private class AddressAdapter extends BaseAdapter {

        public List<AddressBean> beans;

        public AddressAdapter() {

        }

        public void updateList(List<AddressBean> addressBeans) {
            this.beans = addressBeans;
        }

        @Override
        public int getCount() {
            if (beans != null && beans.size() > 0) {
                return beans.size();
            }
            return 0;
        }

        @Override
        public Object getItem(int position) {
            if (beans != null && beans.size() > 0) {
                return beans.get(position);
            }
            return 0;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }


        //TODO 复写该方法
        @Override
        public final boolean hasStableIds() {
            return true;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            AddressBean addressBean = beans.get(position);
            ViewHolder viewHolder;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = View.inflate(context, R.layout.item_address_info, null);
                viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
                viewHolder.tv_address = (TextView) convertView.findViewById(R.id.tv_address);
                viewHolder.tv_address_detail = (TextView) convertView.findViewById(R.id.tv_address_detail);

                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            viewHolder.tv_name.setText(addressBean.getConsigneeName());
            viewHolder.tv_address.setText(addressBean.getProvince() + " " + addressBean.getCity() + " " + addressBean.getCounty() + " ");
            viewHolder.tv_address_detail.setText(addressBean.getAddressDetail());

            return convertView;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (data != null) {
            switch (requestCode) {
                case 0:

                    //TODO listview item 显示地址数据
                    if (resultCode == 0) {
                        String name = data.getStringExtra("name");
                        String province = data.getStringExtra("province");
                        String city = data.getStringExtra("city");
                        String county = data.getStringExtra("county");
                        String addressDetail = data.getStringExtra("addressDetail");
                        AddressBean addressBean = new AddressBean(name, province, city, county, addressDetail);

                        if (!addressBeans.contains(addressBean)) {
                            addressBeans.add(0, addressBean);
                        }

                        listview.manipulate(new AnimationListView.Manipulator<AddressAdapter>() {
                            @Override
                            public void manipulate(AddressAdapter adapter) {
                                adapter.updateList(addressBeans);
                            }
                        });
                    }
                    break;

                case 2:

                    break;
                default:
                    break;
            }
        }
    }

    public static class ViewHolder {
        TextView tv_name;
        TextView tv_address;
        TextView tv_address_detail;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}
